//
//  ACECourseDetailsViewController.m
//  ACE
//
//  Created by mac on 2018/8/16.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "ACECourseDetailsViewController.h"

static NSInteger time1 = 0; //倒计时时间

@interface ACECourseDetailsViewController ()
{
    NSTimer *_timer;
}

@property (weak, nonatomic) IBOutlet UILabel *className;
@property (weak, nonatomic) IBOutlet UILabel *classDescribe;
@property (weak, nonatomic) IBOutlet UIButton *startBtn;
@property (weak, nonatomic) IBOutlet UILabel *startLab;
@property (weak, nonatomic) IBOutlet UILabel *timeLab;
@property (weak, nonatomic) IBOutlet UIButton *endBtn;

@property (nonatomic, assign) NSInteger num;
@property (nonatomic, assign) NSInteger num1;

@end

@implementation ACECourseDetailsViewController

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    //取消定时器
    [_timer invalidate];
    _timer = nil;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.title = @"课程详情";
    
    [self.startBtn addTarget:self action:@selector(startBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    time1 = 0;
}

- (void)startBtnClick:(UIButton *)btn
{
    self.num ++;
    if (self.num % 2 != 0) {
        self.endBtn.hidden = NO;
        [self.endBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.timeLab.mas_bottom).offset(56);
            make.width.equalTo(@114);
            make.left.equalTo(self.view);
            make.bottom.equalTo(self.view).offset(-78);
        }];
        self.num1 = 0;
        [self.endBtn setImage:[UIImage imageNamed:@"end"] forState:UIControlStateNormal];
        [self.endBtn addTarget:self action:@selector(endBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:@"暂停" forState:UIControlStateNormal];
        [self openCountdown];
    } else {
        self.endBtn.hidden = YES;
        [btn setTitle:@"开始" forState:UIControlStateNormal];
        [_timer invalidate];
    }
}

- (void)endBtnClick:(UIButton *)btn
{
    [self.endBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timeLab.mas_bottom).offset(56);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-78);
    }];
    [self.endBtn setImage:[UIImage imageNamed:@"sure_btn"] forState:UIControlStateNormal];
    self.num1 ++;
    if (self.num1 != 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

// 开启倒计时效果
-(void)openCountdown
{
    __weak typeof(self) weakSelf = self;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:weakSelf selector:@selector(timerFire) userInfo:nil repeats:YES];
    [_timer fire];
}
-(void)timerFire
{
    time1++;
    self.timeLab.text = [self getMMSSFromSS:time1];
}

- (NSString *)getMMSSFromSS:(NSInteger )seconds
{
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%ld",seconds/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%ld",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@分钟%@秒",str_minute,str_second];
    
    NSLog(@"format_time : %@",format_time);
    
    return format_time;
}


@end
